/**
 * 
 */
package ro.dta.idbi.ui.sql;

import java.util.ArrayList;
import java.util.List;

import ro.dta.idbi.interfaces.sql.ISqlColumn;
import ro.dta.idbi.interfaces.sql.ISqlForeignKey;
import ro.dta.idbi.interfaces.sql.ISqlTable;
import ro.dta.idbi.model.Utils;
import uk.ac.ic.doc.automed.reps.Schema;
import uk.ac.ic.doc.automed.reps.SchemaObject;

import com.mxgraph.model.mxGeometry;
import com.mxgraph.model.mxICell;

/**
 * Foreign key construct used in the SQL diagram component
 * 
 * @author Tudor Dobrila
 * 
 */
public class SqlForeignKeyCell extends AbstractSqlCell implements ISqlForeignKey, Cloneable {

	private static final long serialVersionUID = 2470421461115996076L;

	/**
	 * Source columns
	 */
	protected transient List<ISqlColumn> sourceCols;

	/**
	 * Target columns
	 */
	protected transient List<ISqlColumn> targetCols;

	/**
	 * @param schema
	 *            Schema containing the foreign key, null if no schema owns it
	 * @param schemaObj
	 *            AutoMed Schema object associated with this construct, null if
	 *            no object is associated with it
	 * @param ident
	 *            ID of construct
	 * @param value
	 *            Value of cell
	 * @param source
	 *            Source of the foreign key
	 * @param target
	 *            Target of the foreign key
	 * @param style
	 *            Style of the cell
	 */
	public SqlForeignKeyCell(final Schema schema, final SchemaObject schemaObj, final String ident,
			final String value, final mxICell source, final mxICell target, final String style) {
		super(schema, schemaObj, ident, value, new mxGeometry(), style);
		setEdge(true);
		getGeometry().setRelative(true);
		setSource(source);
		setTarget(target);

		sourceCols = new ArrayList<ISqlColumn>();
		targetCols = new ArrayList<ISqlColumn>();
	}

	@Override
	public Object clone() throws CloneNotSupportedException {
		final SqlForeignKeyCell obj = (SqlForeignKeyCell) super.clone();
		obj.source = source;
		obj.target = target;

		return obj;
	}

	@Override
	public List<ISqlColumn> getSourceCols() {
		return sourceCols;
	}

	@Override
	public List<ISqlColumn> getTargetCols() {
		return targetCols;
	}

	@Override
	public void addSourceCol(final ISqlColumn column) {
		sourceCols.add(column);

	}

	@Override
	public void addTargetCol(final ISqlColumn column) {
		targetCols.add(column);
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		for (ISqlColumn c : sourceCols) {
			builder.append(c.getShortDefinition()).append(", ");
		}
		builder.setLength(builder.length() - 2);
		builder.append(" -> ");
		for (ISqlColumn c : targetCols) {
			builder.append(c.getShortDefinition()).append(", ");
		}
		builder.setLength(builder.length() - 2);
		return builder.toString();
	}

	@Override
	public String getSchemeDefinition() {
		return Utils.genFKRepresentation(getValue().toString(), (ISqlTable) getSource(),
				(ISqlTable) getTarget(), sourceCols, targetCols);
	}
}
